/*
 * Powered By fanrenqij.com
 * Since 2017 - 2017
 */

package com.fanrenqiji.modules.business.messagetemplate.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.Lists;

import com.fanrenqiji.entity.MessageTemplateEntity;
import com.fanrenqiji.modules.business.messagetemplate.vo.MessageTemplateVO;
import com.fanrenqiji.modules.business.messagetemplate.dao.MessageTemplateDao;
import com.fanrenqiji.modules.business.messagetemplate.service.MessageTemplateService;

import org.dozer.DozerBeanMapper;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.fanrenqiji.common.persistence.Page;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.Order;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * 课程留言板业务逻辑实现
 *
 * @author wentao
 */
@Service(value = "messageTemplate")
@Transactional(readOnly = true)
public class MessageTemplateServiceImpl implements MessageTemplateService{

@Autowired
private MessageTemplateDao messageTemplateDao;

@Autowired
    DozerBeanMapper beanMapper;

/**
 * 插入
 *
 * @param messageTemplate
 * 插入的对象
 * @return 插入数据条数
 */
@Transactional
public MessageTemplateVO insert(MessageTemplateVO messageTemplate){
        MessageTemplateEntity messageTemplateEntity=new MessageTemplateEntity();
        beanMapper.map(messageTemplate,messageTemplateEntity);
        messageTemplateDao.save(messageTemplateEntity);
        beanMapper.map(messageTemplateEntity,messageTemplate);
        return messageTemplate;
        }


/**
 * 批量插入
 *
 * @param list
 * 数据列表
 * @return 插入数据条数
 */
@Transactional
public void insertList(List<MessageTemplateVO>list){
        for(MessageTemplateVO messageTemplate:list){
        insert(messageTemplate);
        }
        }


/**
 * 修改
 *
 * @param messageTemplate
 *            修改的数据
 * @return 修改的数据
 */
@Transactional
public MessageTemplateVO update(MessageTemplateVO messageTemplate){
        MessageTemplateEntity messageTemplateEntity=null;
        if(StringUtils.isNotEmpty(messageTemplate.getId())){
        messageTemplateEntity=messageTemplateDao.findOne(Long.parseLong(messageTemplate.getId()));
        }
        if(messageTemplateEntity==null){
        messageTemplateEntity=new MessageTemplateEntity();
        }
        beanMapper.map(messageTemplate,messageTemplateEntity);
        messageTemplateDao.save(messageTemplateEntity);
        beanMapper.map(messageTemplateEntity,messageTemplate);
        return messageTemplate;
        }


/**
 * 删除
 *
 * @param id
 *            数据标识
 * @return 删除的数据条数
 */
@Transactional
public void delete(String id){
        messageTemplateDao.deleteById(Long.parseLong(id));
        }


/**
 * 查询所有数据
 *
 * @return 数据列表
 */
public List<MessageTemplateVO>getAll(){
        List<MessageTemplateVO>list=Lists.newArrayList();
        List<MessageTemplateEntity>entities=messageTemplateDao.findAllList();
        for(MessageTemplateEntity messageTemplateEntity:entities){
        MessageTemplateVO messageTemplateVO=new MessageTemplateVO();
        beanMapper.map(messageTemplateEntity,messageTemplateVO);
        list.add(messageTemplateVO);
        }
        return list;
        }


/**
 * 通过条件查询数据列表
 *
 * @param messageTemplate
 *            查询条件
 * @return 数据列表
 */
public List<MessageTemplateVO>getList(MessageTemplateVO messageTemplate){
        List<MessageTemplateVO>list=Lists.newArrayList();
        DetachedCriteria dc=messageTemplateDao.createDetachedCriteria();
        dc.add(Restrictions.eq(MessageTemplateEntity.DEL_FLAG,MessageTemplateEntity.DEL_FLAG_NORMAL));
    /*
    dc.createAlias("messageTemplate", "messageTemplate"); // as

    dc.add(Restrictions.ne("id", 1L)); // !=

    if (StringUtils.isNotEmpty(messageTemplate.getName())) {
        dc.add(Restrictions.like("name", "%" + messageTemplate.getName() + "%")); //like
    }
    if (StringUtils.isNotEmpty(messageTemplate.getName())) {
        dc.add(Restrictions.eq("name", messageTemplate.getName())); //=
    }

	dc.addOrder(Order.asc("office.id")).addOrder(Order.asc("name"));  //排序

    */
        List<MessageTemplateEntity>entities=messageTemplateDao.find(dc);
        for(MessageTemplateEntity messageTemplateEntity:entities){
        MessageTemplateVO messageTemplateVO=new MessageTemplateVO();
        beanMapper.map(messageTemplateEntity,messageTemplateVO);
        list.add(messageTemplateVO);
        }
        return list;
        }


/**
 * 通过id查询数据
 *
 * @param id
 *            数据标识
 * @return 数据对象
 */
public MessageTemplateVO get(String id){
        MessageTemplateVO messageTemplate=new MessageTemplateVO();
        MessageTemplateEntity messageTemplateEntity=messageTemplateDao.findOne(Long.parseLong(id));
        if(messageTemplateEntity==null){
        messageTemplateEntity=new MessageTemplateEntity();
        }
        beanMapper.map(messageTemplateEntity,messageTemplate);
        return messageTemplate;
        }


/**
 * 根据条件分页查询
 * @param page
 * @param messageTemplate
 * @return
 */
public Page<MessageTemplateVO>findMessageTemplateByPage(Page<MessageTemplateVO>page,MessageTemplateVO messageTemplate){
        Page<MessageTemplateEntity>pageEntity=new Page<MessageTemplateEntity>(page.getPageNo(),page.getPageSize());
        DetachedCriteria dc=messageTemplateDao.createDetachedCriteria();
        dc.add(Restrictions.eq(MessageTemplateEntity.DEL_FLAG,MessageTemplateEntity.DEL_FLAG_NORMAL));
        if (StringUtils.isNotEmpty(messageTemplate.getName())) {
            dc.add(Restrictions.like("name", "%" + messageTemplate.getName() + "%")); //like
        }
        if (StringUtils.isNotEmpty(messageTemplate.getType())) {
                dc.add(Restrictions.eq("type",Integer.parseInt(messageTemplate.getType())));
        }
    /*
    dc.createAlias("messageTemplate", "messageTemplate"); // as

    dc.add(Restrictions.ne("id", 1L)); // !=


    if (StringUtils.isNotEmpty(messageTemplate.getName())) {
        dc.add(Restrictions.eq("name", messageTemplate.getName())); //=
    }

    if (!StringUtils.isNotEmpty(page.getOrderBy())) { //排序
	    dc.addOrder(Order.asc("name"))
		  .addOrder(Order.desc("id"));
	}
    */
        if(!StringUtils.isNotEmpty(page.getOrderBy())){
            dc.addOrder(Order.desc("id"));
        }
        pageEntity=messageTemplateDao.find(pageEntity,dc);
        beanMapper.map(pageEntity,page);
        List<MessageTemplateVO>list=Lists.newArrayList();
        for(MessageTemplateEntity messageTemplateEntity:pageEntity.getList()){
            MessageTemplateVO messageTemplateVO=new MessageTemplateVO();
            beanMapper.map(messageTemplateEntity,messageTemplateVO);
            list.add(messageTemplateVO);
        }
        page.setList(list);
        return page;
        }

        /**
         * 根据Id查模版内容
         * @param id
         * @return
         */
        public String getContentById(String id){
                return messageTemplateDao.getContentById(Long.parseLong(id));
        }

 }
